package student.collection.list;

import java.util.concurrent.CopyOnWriteArrayList;

/**
 * CopyOnWriteArrayList 每次添加删除会加锁，形成新的集合
 */
public class CopyOnWriteListTest {

    public static void main(String[] args) {
        CopyOnWriteArrayList<String> copyOnWriteArrayList = new CopyOnWriteArrayList<>();

        new Thread(() -> {
            for (int i = 0; i < 1000; i++) {
                copyOnWriteArrayList.add(i + "");
            }
        }, Thread.currentThread().getName()).start();

        new Thread(() -> {
            for (int i = 0; i < copyOnWriteArrayList.size(); i++) {
                System.err.println("第" + (i + 1) + "个元素为：" + copyOnWriteArrayList.get(i));
            }
        }, Thread.currentThread().getName()).start();
    }
}
